<?php
/**
 * 模板消息
 * Created by 上海风车教育科技有限公司.
 * User: 刘红伟
 * Date: 15-12-10
 * Email: 454303753@qq.com
 * File:wechattemplateapi.php
 */
class wechattemplateapi{
    private $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
    private  $token;
    private  $template_id;
    private  $parameter=array();
    private $tem_type=1;
    public function __construct($token,$data=array(),$tem_type=1){
        $this->token=$token;
        $this->tem_type=$tem_type;
        $this->parameter=$data;
    }


    //选择模板
    private  function create_tem(){

        switch($this->tem_type){
            case 1:
              $this->template_id='3dv66qpBQtzf7d3W2c2ru522cqGbbKsED-aFLNxTiIs';
              $result=  $this->pay_template();
                break;
            case 2:
                $this->template_id='nTq0LyxP-XZZlzfWH3GleOKoeoQtwy1txNbaYCQm074';//租车成功通知
                $result=     $this->lend_template();
                break;
            case 3:
                $this->template_id='yKYbrs53LyNl3NRpVKgPqlUpkE5xyFsn8WvNdQKmWpg';//扫码租车通知
                $result=     $this->scan_template();
                break;
            case 4:
                $this->template_id='-xaZnqB-Rwy0Ci-VlglasACQXqUg8IgWUtpaNq1uRac';//还车成功通知
                $result=     $this->back_template();
                break;
        }

        return $result;
    }

    //还车成功通知
    private function back_template(){
        return json_encode(array(
            'touser'=>$this->parameter['openId'],
            'template_id'=>$this->template_id,
            'url'=> $this->parameter['url'],
            'topcolor'=>'#000000',
            'data'=>array(
            'first'=>array('value'=>$this->parameter['first'],'color'=>'#000000'),
            'keyword1'=>array('value'=>$this->parameter['location'],'color'=>'#000000'),
            'keyword2'=>array('value'=>$this->parameter['back_time'],'color'=>'#000000'),
            'keyword3'=>array('value'=>$this->parameter['total'],'color'=>'#000000'),
            'remark'=>array('value'=>$this->parameter['remark'],'color'=>'#000000')
            )
        ));
    }

    //扫码租车通知
    private function scan_template(){

        return json_encode(array(
            'touser'=>$this->parameter['openId'],
            'template_id'=>$this->template_id,
            'url'=> $this->parameter['url'],
            'topcolor'=>'#000000',
            'data'=>array(
            'first'=>array('value'=>$this->parameter['first'],'color'=>'#000000'),
            'keyword1'=>array('value'=>$this->parameter['location'],'color'=>'#000000'),
            'keyword2'=>array('value'=>$this->parameter['bike_name'],'color'=>'#000000'),
            'remark'=>array('value'=>$this->parameter['remark'],'color'=>'#000000')
            )
        ));
    }
    //租车模板内容
    private function lend_template(){
            return json_encode(array(
                'touser'=>$this->parameter['openId'],
                'template_id'=>$this->template_id,
                'url'=> $this->parameter['url'],
                'topcolor'=>'#000000',
                'data'=>array(
                'first'=>array('value'=>$this->parameter['first'],'color'=>'#000000'),
                'keyword1'=>array('value'=>$this->parameter['location'],'color'=>'#000000'),
                'keyword2'=>array('value'=>$this->parameter['bike_no'],'color'=>'#000000'),
                'keyword3'=>array('value'=>$this->parameter['price'],'color'=>'#000000'),
                'keyword4'=>array('value'=>$this->parameter['deposit'],'color'=>'#000000'),
                'keyword5'=>array('value'=>$this->parameter['create_time'],'color'=>'#000000'),
                'remark'=>array('value'=>$this->parameter['remark'],'color'=>'#000000')
                )
            ));
    }
    //充值模板内容
    private  function pay_template(){
      //写模板内容
        return json_encode(array(

                        'touser'=>$this->parameter['openId'],
                        'template_id'=>$this->template_id,
                        'url'=> $this->parameter['url'],
                        'topcolor'=>'#000000',
                        'data'=>array(
                                'first'=>array('value'=>$this->parameter['first'],'color'=>'#000000'),
                                'accountType'=>array('value'=>$this->parameter['accountType'],'color'=>'#000000'),
                                'account'=>array('value'=>$this->parameter['account'],'color'=>'#000000'),
                                'amount'=>array('value'=>$this->parameter['amount'],'color'=>'#000000'),
                                'result'=>array('value'=>$this->parameter['result'],'color'=>'#000000'),
                                'remark'=>array('value'=>$this->parameter['remark'],'color'=>'#000000')
                            )
                    )
                );
    }





    //发送模板消息
    public function send(){
        $this->url=$this->url.$this->token;
        $http 	= wx_http::factory($this->url, wx_http::TYPE_CURL);
        $json 	= $http->post($this->url, $this->create_tem(), array(
            'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: '. strlen($this->create_tem())
        ));

        $result	=json_decode($json,true);
        if(!empty($result) && $result['errcode']==0 and $result['errmsg']=='ok'){
             return true;
        }else{
            return false;
        }
    }
}